"""
数据提取策略
"""
import logging

from com.data.cache.cache import DictCache
from com.data.exception.exception import UnSupportHttpMethodException
from com.data.extract import HttpMethodEnum

cache = DictCache()


class Strategy:
    def call(self, http_request):
        method = http_request.method
        if http_request.cookie.__len__() == 0:
            try:
                http_request.cookie = cache.get("cookie")
            except KeyError:
                logging.info("cookie is not exist...")

        if method == HttpMethodEnum.HttpMethodEnum.POST:
            result = self.post(self, http_request)
        elif method == HttpMethodEnum.HttpMethodEnum.GET:
            result = self.get(self, http_request)
        elif method == HttpMethodEnum.HttpMethodEnum.HEAD:
            result = self.head(self, http_request)
        elif method == HttpMethodEnum.HttpMethodEnum.OPTIONS:
            result = self.options(self, http_request)
        elif method == HttpMethodEnum.HttpMethodEnum.PUT:
            result = self.put(self, http_request)
        elif method == HttpMethodEnum.HttpMethodEnum.PATCH:
            result = self.patch(self, http_request)
        elif method == HttpMethodEnum.HttpMethodEnum.DELETE:
            result = self.delete(self, http_request)
        elif method == HttpMethodEnum.HttpMethodEnum.TRACE:
            result = self.trace(self, http_request)
        elif method == HttpMethodEnum.HttpMethodEnum.CONNECT:
            result = self.connect(self, http_request)
        else:
            raise UnSupportHttpMethodException.UnSupportHttpMethodException("未提供http请求方法")
        self.wrapper_cookie(self, result)
        return result

    def post(self, http_request):
        pass

    def get(self, http_request):
        pass

    def head(self, http_request):
        pass

    def options(self, http_request):
        pass

    def put(self, http_request):
        pass

    def patch(self, http_request):
        pass

    def delete(self, http_request):
        pass

    def trace(self, http_request):
        pass

    def connect(self, http_request):
        pass

    def wrapper_cookie(self, result):
        cookies = {}

        try:
            set_cookie = result.headers["Set-Cookie"]
            if set_cookie.__contains__("JSESSIONID"):
                cookies["JSESSIONID"] = self.un_wrapper_cookie("JSESSIONID", set_cookie)
            if set_cookie.__contains__("u="):
                cookies["u"] = self.un_wrapper_cookie("u", set_cookie)
        except KeyError:
            pass

        if cookies.__len__() != 0:
            try:
                cookies["JSESSIONID"]
            except KeyError:
                cookies["JSESSIONID"] = cache.get("cookie")["JSESSIONID"]
            try:
                cookies["u"]
            except KeyError:
                try:
                    cookies["u"] = cache.get("cookie")["u"]
                except KeyError:
                    pass
            cache.put("cookie", cookies)

    def un_wrapper_cookie(key, cookie):
        arr = cookie.split(";")
        for a in arr:
            arr_2 = a.split("=")
            for i in range(len(arr_2)):
                u_key = arr_2[i]
                if arr_2[i].__contains__(","):
                    u_key = arr_2[i].rsplit(",", 2)[1]
                if key.__eq__(u_key.strip()):
                    return arr_2[i + 1]
        return None


if __name__ == "__main__":
    pass
